package com.wdzl.util;

import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class MyPlayer {
    private static Player player;
    /**
     * 播放音乐的方法
     */
    public static void playMusic() {
        try {
            //获取音乐文件
            String musicPath = "music/bj.mp3";
            InputStream is = getSrcFileInputStream(musicPath);
            player = new Player(is);
            //播放音乐
            player.play();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void stop(){
        if(player!=null){
            player.close();
        }
    }

    private static InputStream getSrcFileInputStream(String filename){
        if(!filename.startsWith("/")){
            filename = "/"+filename;
        }
        System.out.println(filename);
        return FileUitl.class.getResourceAsStream(filename);
    }
}
